<%@ page language="java" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@page import="support.*, java.util.*" %>
<html:html>

<body>
<% 
   	support s = new support();   	
   	
   	String path1 = config.getServletContext().getRealPath("countries.txt");
   	String path2 = config.getServletContext().getRealPath("universities.txt");
    String path3 = config.getServletContext().getRealPath("majors.txt");
    String path4 = config.getServletContext().getRealPath("specializations.txt");
	
    Vector countries = s.getCountries(path1);    
    Vector universities = s.getUniversities(path2);
    Vector majors = s.getMajors(path3);
    Vector specializations = s.getSpecializations(path4);
%>
    <%-- Import the java.sql package --%>
            <%@ page import="java.sql.*"%>
            <%-- -------- Open Connection Code -------- --%>
            <%
            
            Connection conn = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            int rowCount =0; 
           try{
                // Registering Postgresql JDBC driver with the DriverManager
                Class.forName("org.postgresql.Driver");

                // Open a connection to the database using DriverManager
                conn = DriverManager.getConnection(
                		"jdbc:postgresql://localhost:5433/GradApp?" +
                        "user=postgres&password=20880130");
           
                
                int i=0,j=0;
                //conn.setAutoCommit(false);
                for(i=0; i<countries.size(); i++){
                pstmt = conn.prepareStatement("INSERT INTO Countries (name) VALUES (?)");
                pstmt.setString(1, (String)countries.get(i));           
                pstmt.executeUpdate();
                //conn.commit();
                }
                
                for (i=0; i<universities.size(); i++){
                	Vector tuple = (Vector)universities.get(i);
                    String state = (String)tuple.get(0);
                    Vector u = (Vector)tuple.get(1);
                    for(j=0; j<u.size(); j++){
                pstmt = conn.prepareStatement("INSERT INTO Universities (location,name) VALUES (?,?)");
                pstmt.setString(1, state);
                pstmt.setString(2, (String)u.get(j));
                pstmt.executeUpdate();
                //conn.commit();
                    }
                }
                
                
                for(i=0; i<majors.size(); i++){
                pstmt = conn.prepareStatement("INSERT INTO Discipline (name) VALUES (?)");
                pstmt.setString(1, (String)majors.get(i));     
                pstmt.executeUpdate();
                //conn.commit();
                }
                
                for(i=0; i<specializations.size(); i++){
				pstmt = conn.prepareStatement("INSERT INTO Specialization (name) VALUES (?)");
				pstmt.setString(1, (String)specializations.get(i));   
                pstmt.executeUpdate();
                //conn.commit();
                }
                
                //conn.setAutoCommit(true);
                
           
           } catch (SQLException e) {

               // Wrap the SQL exception in a runtime exception to propagate
               // it upwards
               throw new RuntimeException(e);
           }
           finally {
               // Release resources in a finally block in reverse-order of
               // their creation

               if (rs != null) {
                   try {
                       rs.close();
                   } catch (SQLException e) { } // Ignore
                   rs = null;
               }
               if (pstmt != null) {
                   try {
                       pstmt.close();
                   } catch (SQLException e) { } // Ignore
                   pstmt = null;
               }
               if (conn != null) {
                   try {
                       conn.close();
                   } catch (SQLException e) { } // Ignore
                   conn = null;
               }
           }
   %>
    
</body>
</html:html>